package com.jl15988.chainlog.example;

import com.jl15988.chainlog.annotation.ChainCustom;
import com.jl15988.chainlog.annotation.ChainMethod;
import com.jl15988.chainlog.manual.ChainLogHelper;
import com.jl15988.chainlog.manual.ChainLogSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 示例服务类
 */
@Service
public class ExampleService {

    @Autowired
    private ExampleRepository exampleRepository;

    /**
     * 简单数据处理
     */
    @ChainMethod(name = "数据处理", desc = "处理用户输入数据")
    public String processData(String data) throws InterruptedException {
        // 模拟数据验证
        validateData(data);

        // 模拟数据转换
        String transformedData = ChainLogHelper.call(ExampleService.class, ExampleService::transformData, data);
        String call = ChainLogHelper.call(ExampleService.class, exampleService -> exampleService.processStep1(data));
        processStep4(data);
//        String transformedData = transformData(data);

        // 模拟数据保存
        exampleRepository.saveData(transformedData);

        return "处理完成: " + transformedData;
    }

    /**
     * 复杂数据处理
     */
    @ChainMethod(
            name = "复杂数据处理",
            desc = "处理复杂业务逻辑",
            customs = {
                    @ChainCustom(key = "user", value = "zhangsan")
            }
    )
    public String processComplexData(String data) {
        // 模拟多步骤处理
        String step1Result = processStep1(data);
        String step2Result = processStep2(step1Result);
        String finalResult = processStep3(step2Result);

        return finalResult;
    }

    /**
     * 创建订单
     */
    @ChainMethod(name = "订单创建", desc = "创建新订单")
    public String createOrder(String orderData) {
        // 模拟订单验证
        validateOrder(orderData);

        // 模拟库存检查
        checkInventory(orderData);

        // 模拟订单保存
        String orderId = exampleRepository.saveOrder(orderData);

        // 模拟发送通知
        sendNotification(orderId);

        return orderId;
    }

    @ChainMethod(name = "数据验证", desc = "验证输入数据格式")
    private void validateData(String data) {
        if (data == null || data.trim().isEmpty()) {
            throw new IllegalArgumentException("数据不能为空");
        }
        // 模拟验证逻辑
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @ChainMethod(name = "数据转换", desc = "转换数据格式")
    public String transformData(String data) {
        System.out.println("--数据转换--");
        // 模拟转换逻辑
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return data.toUpperCase();
    }

    @ChainMethod(name = "步骤1", desc = "处理步骤1")
    public String processStep1(String data) {
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Step1:" + data;
    }

    @ChainMethod(name = "步骤2", desc = "处理步骤2")
    private String processStep2(String data) {
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Step2:" + data;
    }

    @ChainMethod(name = "步骤3", desc = "处理步骤3")
    private String processStep3(String data) {
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        return "Step3:" + data;
    }

    public String processStep4(String data) {
        ChainLogSession chainLogSession = ChainLogHelper.start("步骤4");
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            chainLogSession.endException(e);
        } finally {
            chainLogSession.close();
        }
        // 自动回收
        try(ChainLogSession ignored = ChainLogHelper.start("步骤4 - 2")) {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            chainLogSession.endException(e);
        }
        return "Step3:" + data;
    }

    @ChainMethod(name = "订单验证", desc = "验证订单数据")
    private void validateOrder(String orderData) {
        // 模拟订单验证
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @ChainMethod(name = "库存检查", desc = "检查商品库存")
    private void checkInventory(String orderData) {
        // 模拟库存检查
        try {
            Thread.sleep(20);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    @ChainMethod(name = "发送通知", desc = "发送订单通知")
    private void sendNotification(String orderId) {
        // 模拟发送通知
        try {
            Thread.sleep(15);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}